package com.qf.fmall2302.wxpay;

import java.util.*;
import java.util.stream.Collectors;

public class StreamTest {

    public static void main(String[] args) {

        MonthData data1 = new MonthData("1990-01", 1000l);
        MonthData data2 = new MonthData("1990-02", 100l);
        MonthData data3 = new MonthData("1991-01", 400l);
        MonthData data4 = new MonthData("1991-02", 200l);

        List<MonthData> list = Arrays.asList(data1, data2, data3, data4);

        Map<String, List<MonthData>> map = list.stream()
                .collect(Collectors.groupingBy(monthData -> monthData.getYearMonth().substring(0, 4)));

        System.out.println(map);

        ArrayList<HashMap<String, Object>> result = new ArrayList<>();
        for (Map.Entry<String, List<MonthData>> entry : map.entrySet()) {
            String year = entry.getKey();
            List<MonthData> dataList = entry.getValue();
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("year",year);
            List<Long> salesList = dataList.stream().map(MonthData::getSales).collect(Collectors.toList());
            hashMap.put("salesList",salesList);
            result.add(hashMap);
        }
        System.out.println(result);

    }


}

class MonthData{
    private String yearMonth;

    public Long sales;

    public MonthData() {
    }

    public MonthData(String yearMonth, Long sales) {
        this.yearMonth = yearMonth;
        this.sales = sales;
    }

    @Override
    public String toString() {
        return "MonthData{" +
                "yearMonth='" + yearMonth + '\'' +
                ", sales=" + sales +
                '}';
    }

    public String getYearMonth() {
        return yearMonth;
    }

    public void setYearMonth(String yearMonth) {
        this.yearMonth = yearMonth;
    }

    public Long getSales() {
        return sales;
    }

    public void setSales(Long sales) {
        this.sales = sales;
    }
}